#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:chengyanan
@file: excel_util.py
@time: 2019/12/22  7:27 下午
"""
import os
import sys

path = os.path.dirname(os.getcwd())
sys.path.append(path)
import xlrd
from xlutils.copy import copy


class ExcelUtile:
    def __init__(self, excel_path=None, index=None):
        if excel_path == None:
            self.excel_path = "../config/casedate.xls"
        else:
            self.excel_path = excel_path
        if index == None:
            index = 0
        self.data = xlrd.open_workbook(self.excel_path)
        self.table = self.data.sheets()[index]

    def get_data(self):
        result = []
        rows = self.get_lines()
        if rows != None:
            for i in range(self.get_lines()):
                col = self.table.row_values(i)
                result.append(col)
            return result
        return None

    # 获取行数
    def get_lines(self):
        rows = self.table.nrows
        if rows >= 1:
            return rows
        return None

    # 获取单元格的内容
    def get_col_value(self, row, col):
        if self.get_lines() > row:
            data = self.table.cell(row, col).value
            return data
        return None

    def write_value(self, row, value):
        read_value = xlrd.open_workbook(self.excel_path)
        write_data = copy(read_value)
        write_data.get_sheet(0).write(row, 9, value)
        write_data.save("../report/keyword_result.xls")


if __name__ == '__main__':
    es = ExcelUtile(excel_path="../config/keyword.xls")
    print(es.get_col_value(1, 3))
